

import React from 'react'
import { Menu } from 'antd';
import { routerList } from '../../utils/router';
import { withRouter } from "react-router-dom"

function Side({ location, history }: any) {
    const { SubMenu } = Menu;
    const createMenuItem = (list: any) => {
        return list.map((l: any) => {
            if (l.children) {
                return (<SubMenu key={l.path} icon={l.icon} title={l.text} >
                    {createMenuItem(l.children)}
                </SubMenu>)
            } else {
                return (<Menu.Item key={l.path} icon={l.icon}>{l.text}</Menu.Item>)
            }
        })
    }
    const changeRoute = ({ key }: any) => {
        history.push(key)
    }
    return (
        <div>
            <Menu
                defaultSelectedKeys={[location.pathname]}
                defaultOpenKeys={["/" + location.pathname.split("/").splice(1, 2).join("/")]}
                mode="inline"
                theme="dark"
                onClick={changeRoute}
            >
                {createMenuItem(routerList)}
            </Menu>
        </div>
    )
}

export default withRouter(Side)
